/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2025-01-06 15:21:54
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2025-07-23 07:52:51
 * @FilePath: \wire_receiver_main\app\task_misc.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "task_manager.h"
#include "main.h"
#include "com_scan.h"
#include "com_lora.h"

#if 0
        LED_GREEN(1);	
        LED_RED(1);
        vTaskDelay(100);
        LED_GREEN(0);	
        LED_RED(0);
#endif

#define ENCODER_PLUS_MAX 400.0f // 编码器一圈脉冲数
#define PLATE_DIAMETER   0.063f   // 盘子的直径(单位: m)
#define PI 3.1415926f        // PI
#define UNIT_PLUS_DISTANCE  (PLATE_DIAMETER * PI / ENCODER_PLUS_MAX) // 一个脉冲的距离

void task_misc(void *pvParameters)
{
    TickType_t voice_times = xTaskGetTickCount();
    TickType_t scan_times = xTaskGetTickCount();

//drv1_line_len_mm = (PLATE_DIAMETER * PI / ENCODER_PLUS_MAX);

    while (1)
    {
        /* 播报语音检测 */
#if 0
        #define VOICE_KX_PLAY_TIMES 2000U
        if (master_lora.key[0].stop_sta == 0x01) // 收线已停止
        {
            if (xTaskGetTickCount() - voice_times >= VOICE_KX_PLAY_TIMES)
            {
                VOICE_K2_PLAY(0);
                master_lora.key[0].stop_sta == 0x00;
            }
            else{
                VOICE_K2_PLAY(1);
            }
        }
        else if (master_lora.key[0].slow_sta == 0x01) // 请求换盘中
        {
            if (xTaskGetTickCount() - voice_times >= VOICE_KX_PLAY_TIMES)
            {
                VOICE_K3_PLAY(0);
                master_lora.key[0].stop_sta == 0x00;
            }
            else{
                VOICE_K3_PLAY(1);
            }
        }
        else
        {
            voice_times = xTaskGetTickCount();
        }
#endif
        /* 1000ms 定时发消息给屏幕 */
        if (xTaskGetTickCount() - scan_times > 1000)
        {
            scan_times = xTaskGetTickCount();
            com_scan_send_change_sensor();
        }

        /* 200ms循环发送lora数据 */
        com_lora_tx_hanlder();

        /* 自动检测逻辑使能 */
        if(master_lora.flag_on_use == 0x01)
        {
            /* 设备1 减速距离 和 停止距离 都设置过 */
            if((scan_set_info.drv1_slow_line_len != 0) && \
               (scan_set_info.drv1_stop_line_len != 0))
            {
                /* 减速 阈值控制在1米以内 */
                #define LINE_LEN_LIMIT 1.0f
                if(((float)scan_set_info.drv1_slow_line_len - master_lora.drv1_len_m) <= LINE_LEN_LIMIT )
                {
                    master_lora.key[0].slow_ctrl = 0x01;
                    master_lora.key[0].stop_ctrl = 0x00;
                }   
                /* 停止 */
                if(((float)scan_set_info.drv1_stop_line_len - master_lora.drv1_len_m) <= LINE_LEN_LIMIT)
                {
                    master_lora.key[0].slow_ctrl = 0x00;
                    master_lora.key[0].stop_ctrl = 0x01;
                } 
            }
#if 0
            /* 设备2 减速距离 和 停止距离 都设置过 */
            if((scan_set_info.drv2_slow_line_len != 0) && \
               (scan_set_info.drv2_stop_line_len != 0))
            {
                /* 减速 */
                if(scan_set_info.drv2_slow_line_len == 0)
                {

                }   
                /* 停止 */
                else if(scan_set_info.drv2_stop_line_len == 0)
                {

                } 
            }
#endif
        }

        vTaskDelay(10);
    }
}
